<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name' => 'required',
            'role_id' => 'required',
            'email'  => 'required|unique:users',
            'password' => 'required'
        ];
        // 编辑的话，规则有修改
        if ($this->isMethod('put')) {
            $id = $this->route('admin');
            $rules['email'] = "sometimes|required|unique:users,email,{$id}";
            unset($rules['password']);
        }

        return $rules;
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '名称必填',
            'email.unique' => '邮箱唯一',
            'email.required' => '登陆邮箱必填',
            'role.required' => '角色必填',
            'password.required' => '密码必填'
        ];
    }
}
